package 掘金.二进制之和;

import java.math.BigInteger;
import java.util.ArrayList;
import java.util.List;

public class Main {

    // 十进制字符串加法
    public static String addD(String a, String b){
        int len1 = a.length();
        int len2 = b.length();
        int len = Math.min(len1,len2);
        StringBuilder sr = new StringBuilder();
        int flag = 0;
        for(int i=0;i<len;i++){
            int t = flag;
            t += a.charAt(len1-i-1)-'0';
            t += b.charAt(len2-i-1)-'0';
            flag = t/10;
            t %= 10;
            sr.insert(0,t);
        }
        if(len1<len2){
            for(int i=len1;i<len2;i++){
                int t = b.charAt(len2-i-1)-'0';
                t += flag;
                flag = t/10;
                t %= 10;
//                if(t!=0|| flag!=0)
                    sr.insert(0,t);
            }
        }else if(len1>len2){
            for(int i=len2;i<len1;i++){
                int t = a.charAt(len1-i-1)-'0';
                t += flag;
                flag = t/10;
                t %= 10;
//                if(t!=0|| flag!=0)
                    sr.insert(0,t);
            }
        }
        if(flag>0)
            sr.insert(0,flag);
        return sr.toString();
    }

    public static String solution(String binary1, String binary2) {
        // Please write your code here
        StringBuilder sr = new StringBuilder();
        boolean flag = false;   // 是否有进位
        int len1 = binary1.length();
        int len2 = binary2.length();
        int sub_len = Math.min(len1,len2);
        for(int i=0;i<sub_len;i++){
            // 8 种
            int a = binary1.charAt(len1-i-1) - '0';
            int b = binary2.charAt(len2-i-1) - '0';
            int t = a + b + (flag==true? 1:0);
            flag = t >=2;
            t %=2;
            if(t==0)
                sr.insert(0, "0");
            else
                sr.insert(0, "1");
        }
        if(len1>len2){
            for (int i = len2; i < len1; i++) {
                if(flag == false){ // 没有进位，则直接将其补上即可
                    sr.insert(0,binary1.substring(0,len1-i));
                    break;
                }
                int a = binary1.charAt(len1-i-1) - '0';
                int t = a + (flag==true? 1:0);
                flag = t >=2;
                t %=2;
                if(t==0)
                    sr.insert(0, "0");
                else
                    sr.insert(0, "1");
            }

        }else if(len1<len2) {
            for (int i = len1; i < len2; i++) {
                if(flag == false){ // 没有进位，则直接将其补上即可
                    sr.insert(0,binary2.substring(0,len2-i));
                    break;
                }
                int b = binary2.charAt(len2-i-1) - '0';
                int t = b + (flag==true? 1:0);
                flag = t >=2;
                t %=2;
                if(t==0)
                    sr.insert(0, "0");
                else
                    sr.insert(0, "1");
            }
        }
        if(flag)
            sr.insert(0, "1");
         System.out.println("2b:"+ sr);
        // 二进制字符串 转 十进制字符串
        // 计算出每一位对应的十进制数的字符串，然后相加，最后得到真正的十进制数
        // 那还得写十进制字符串加法
        List<String> list = new ArrayList<>();
        StringBuilder t = new StringBuilder("1");
        for(int i=0;i<sr.length();i++){
            int b = sr.charAt(sr.length()-i-1)-'0';
            if(b==1){
                list.add(t.toString());
            }
            t = new StringBuilder(addD(t.toString(), t.toString()));
        }
        String res = "0";
        for (int i = 0; i < list.size(); i++) {
            res = addD(res, list.get(i));
        }

        return res;
    }

    public static void main(String[] args) {
        // You can add more test cases here
        //System.out.println(solution("101", "110").equals("11"));
        //System.out.println(solution("111111", "10100").equals("83"));
        //System.out.println(solution("111010101001001011", "100010101001").equals("242420"));
        System.out.println(solution("101011101111110110001000", "111010011100").equals("11471908"));

        System.out.println(addD("000","1"));
    }
}